<?php
/**
 * Created by PhpStorm.
 * User: longli
 * VX: isa1589518286
 * Date: 2021/08/28
 * Time: 23:38
 * @link http://www.lmterp.cn
 */

namespace app\admin\controller\cha;

use app\admin\controller\BaseController;
use app\common\model\Channel;
use app\common\model\ChannelAllowAttr;
use app\common\model\ChannelAttr;
use app\common\service\logistics\ChannelService;

/**
 * 渠道可走属性
 * Class AllowController
 * @package app\admin\controller\cha
 */
class AllowController extends BaseController
{

    /**
     * 首页
     * @date 2021/08/29
     * @author longli
     */
    public function index()
    {
        $this->assign("is_allow_add", $this->user->hasPermissions("/cha/allow/add"));
        if($this->request->isAjax())
        {
            $allow = ChannelAllowAttr::with(["channel"]);
            $this->searchModel($allow, [
                'eq' => ['channel_id'],
                'times' => ['create_time'],
            ]);
            $allow->order("allow_id desc");
            $limit = $this->getPageSize();
            $allow = $allow->paginate($limit);
            $this->assign("is_show_delete", $this->user->hasPermissions("/cha/allow/delete"));
            $this->assign('list', $allow->getCollection());
            $this->assign('page', $allow->render());
            return $this->fetch('lists');
        }
        $this->assign("channel", Channel::getAll());
        return $this->fetch('index');
    }


    /**
     * 添加配置
     * @date 2021/08/29
     * @author longli
     */
    public function add()
    {
        if(($id = $this->request->request("allow_id")) && ($allow = ChannelAllowAttr::get($id)))
        {
            $this->assign('allow', $allow);
            if(!empty($allow->include_attr))
            {
                $this->assign('include_attr', ChannelAttr::field(["attr_id", "attr_name"])
                    ->whereIn("attr_id", $allow->include_attr)
                    ->select()
                    ->toArray()
                );
            }
            if(!empty($allow->exlude_attr))
            {
                $this->assign('exlude_attr', ChannelAttr::field(["attr_id", "attr_name"])
                    ->whereIn("attr_id", $allow->exlude_attr)
                    ->select()
                    ->toArray()
                );
            }
            if(!empty($allow->include_sku))
            {
                $tempSku = explode(',', $allow->include_sku);
                $skus = [];
                foreach($tempSku as $sku)
                    $skus[] = compact('sku');
                $this->assign('include_sku', $skus);
            }
            if(!empty($allow->exlude_sku))
            {
                $tempSku = explode(',', $allow->exlude_sku);
                $skus = [];
                foreach($tempSku as $sku)
                    $skus[] = compact('sku');
                $this->assign('exlude_sku', $skus);
            }
        }
        $this->assign('channel', Channel::getAll());
        return $this->fetch('add');
    }

    /**
     * 保存配置
     * @date 2021/08/29
     * @author longli
     */
    public function save()
    {
        $service = ChannelService::getInstance();
        $allowData = [];
        if($this->request->isPost())
        {
            $validate = \think\facade\Validate::make([
                "channel_id" => "require"
            ], [
                "channel_id.require" => "渠道必选"
            ]);
            $allowData = $this->request->post();
            if(!$validate->batch()->check($allowData)) $this->error(join(', ', $validate->getError()));
            if(!isset($allowData['allow_id'])
                && ChannelAllowAttr::where("channel_id", $allowData['channel_id'])->count()
            ) $this->error("渠道已配置过");
            if(!isset($allowData['status'])) $allowData['status'] = ChannelAllowAttr::IS_NO;
        }
        else
        {
            $allowData = $this->request->only(['status', 'allow_id'], 'get');
        }
        $service->addAllow($allowData)
            ? $this->success("操作成功")
            : $this->error("操作失败");
    }

    /**
     * 删除配置
     * @date 2021/08/29
     * @author longli
     */
    public function delete()
    {
        $ids = $this->request->get("ids");
        if(empty($ids)) $this->error("非法操作");
        ChannelAllowAttr::destroy($ids);
        $this->success("删除成功");
    }
}